<!--
@description ：黑名单管理
-->

<template>
  <div class="app-container">
    <el-card>
      <h3>黑名单管理</h3>
    </el-card>
    <el-card style="margin-top: 20px;">
      <NormalForm :enumOpt="enumOpt" :formList="formList" :isShowCard="false" :operateForm="queryForm"
                  pageTitle=""
                  @handleQuery="handleQuery" @handleReset="handleReset"></NormalForm>
    </el-card>
    <el-card style="margin-top: 20px">
      <div class="right-btn" style="margin-right: 0; padding: 0 0 16px 0">
        <el-button plain size="mini" type="info" @click="batchShiftOut" v-hasPermi="['admin:blacklist:remove']">批量移出黑名单
        </el-button>
        <el-button size="mini" type="primary" @click="join" v-has-permi="['admin:blacklist:add']">加入黑名单
        </el-button>
      </div>
      <el-table ref="table" v-loading="loading" :data="tableList" max-height="560" min-height="420"
                style="width: 100%" @select="handleSelectionChange" @select-all="handleSelection" v-has-permi="['admin:blacklist:list']">
        <el-table-column align="center" fixed type="selection" width="45"></el-table-column>
        <el-table-column :show-overflow-tooltip="false" align="center" label="客户" min-width="200"
                         prop="wechatNickname">
          <template slot-scope="{ row }">
            <div style="display: flex;align-items: center;justify-content: center">
              <img v-if="row.avatar" :src="row.avatar" alt=""
                   style="width:30px; height:30px;border-radius:50%;margin-right:6px">
              <span style="line-height: 30px">{{ row.wechatNickname }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="false" align="center" label="备注" min-width="140"
                         prop="remark">
          <template slot-scope="{ row }">
            <el-tooltip :content="row.remark" height="36" placement="top" title=" " trigger="hover" width="300">
              <div style="cursor: pointer;color:#082AFF">查看</div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="false" align="center" label="操作人" min-width="140"
                         prop="createBy"></el-table-column>
        <el-table-column :show-overflow-tooltip="false" align="center" label="操作时间" min-width="170"
                         prop="createTime"></el-table-column>
        <el-table-column label="操作" prop="operation" width="120">
          <template slot-scope="scope">
            <el-button v-hasPermi="['admin:blacklist:remove']" size="medium" type="text" @click="shiftout(scope.row.blackId)">移出黑名单</el-button>
          </template>
        </el-table-column>
      </el-table>
      <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total"
                        @handleCurrentChange="handleCurrentChange" :page-size-list="pageSizeList"
                        @handleSizeChange="handleSizeChange"></NormalPagination>
    </el-card>

    <select-supermicro-dialog :visible.sync="selectSupermicroVisible"
                              @supermicroUpdate="supermicroUpdate"></select-supermicro-dialog>
    <el-dialog :close-on-click-modal="false" :title="selectCustomerTitle" :visible.sync="selectCustomerVisible" width="1120px" modal-append-to-body>
      <select-customer :supermicroList.sync="supermicroList" ref="selectCustomer"  @customerUpdate="customerListUpdate"></select-customer>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="selectCustomerVisible = false" :loading="addLoading">取 消</el-button>
        <el-button size="mini" type="primary" @click="customerUpdate" :loading="addLoading">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :show-close="true"
               :visible.sync="remarkVisible"
               title="备注" width="520px">
      <el-form ref="remarkForm" :model="remarkForm" label-width="0">
        <el-form-item label="" prop="remark">
          <el-input v-model="remarkForm.remark" clearable maxlength="200" rows="4" show-word-limit
                    type="textarea" style="width: 460px" placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="remarkVisible = false" :loading="saveLoading">取 消</el-button>
        <el-button :loading="saveLoading" type="primary" @click="saveDispose">保存</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="delvisible" title="移出黑名单" width="320px">
      <div style="text-align: center;height: 50px;line-height: 50px;">
        <i class="el-icon-question"
           style="color: #082AFF;margin-right: 10px;"></i><span>确定要将这些客户移出黑名单吗?</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="delloading" size="mini" @click="delvisible = false">取 消</el-button>
        <el-button :loading="delloading" size="mini" type="primary" @click="deleteConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>

import SelectCustomer from "@/components/selectCustomer.vue";
import SelectSupermicroDialog from "@/components/selectSupermicroDialog.vue";
import * as api from '@/api/organization'
import * as $api from "@/api/riskManager/blackList";

export default {
  name: "blackListManager", //页面名称，必填
  props: {},
  components: {
    SelectSupermicroDialog,
    SelectCustomer,
  },
  data() {
    return {
      queryForm: {
        userName: '',
        weUserId: "",
        pageNum: 1,
        pageSize: 10,
      },
      pageSizeList: [10,20,30,50],
      formList: [
        {
          label: '客户名称',
          value: '',
          prop: 'userName',
          type: 'INPUT',
          emunName: '',
        },
        {
          label: '操作员工',
          value: [],
          prop: 'weUserId',
          type: 'SELECT',
          emunName: 'weUserIdList',
          multiple: false,
          defaultNoLabel: true,
        }
      ], // 查询框
      enumOpt: {
        weUserIdList: [],
      },
      tableList: [],
      selectedList: [],
      loading: false,
      total: 0,
      supermicroList: [], //选中的超级企微数据
      selectSupermicroVisible: false, // 展示超级企微选择弹窗
      selectCustomerVisible: false, // 展示客户选择弹窗
      addLoading: false,
      selectCustomerTitle: '选择黑名单客户',
      delvisible: false, // 删除选择弹窗
      delloading: false,
      selectedCustomerList: [],

      saveLoading: false,
      remarkVisible: false,
      remarkForm: {
        remark: '',
      }
    }
  },
  watch: {},
  methods: {
    handleQuery() {
      this.queryForm.pageNum = 1
      this.getListFn()
    },
    handleReset() {
       this.queryForm = {
         userName: '',
         weUserId: "",
         pageNum: 1,
         pageSize: 10,
       }
       this.getListFn()
    },
    getListFn() {
      let obj = {
        ...this.queryForm
      }
      this.loading = true
      this.tableList = []
      $api.getBlacklist(obj).then(res => {
        if(res.code == 200){
          this.tableList = res.rows
          this.total = res.total
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.getListFn()
    },
    handleSizeChange(val) {
      this.queryForm.pageNum = 1
      this.queryForm.pageSize = val
      this.getListFn()
    },
    handleSelection(val){
      this.selectedList = val
    },
    handleSelectionChange(val) {
      this.selectedList = val
    },
    // 移除黑名单
    shiftout(val) {
      this.delvisible = true
      this.selectedList = [{blackId: val} ]
    },
    // 批量移出
    batchShiftOut() {
      this.delvisible = true
    },
    // 加入黑名单
    join() {
      this.supermicroList = []
      this.selectSupermicroVisible = true
    },
    // 超级企微数据选中
    supermicroUpdate(val) {
      console.log('超级企微选中的数据:::::', val)
      this.selectSupermicroVisible = false
      this.supermicroList = val
      this.selectedCustomerList = []
      this.selectCustomerVisible = true
      this.$nextTick(() => {
        this.$refs.selectCustomer.clearSelected()
      })
    },
    // 客户数据选中
    customerListUpdate(val) {
       this.selectedCustomerList = val
    },
    customerUpdate(){
      this.remarkForm.remark = ''
      this.remarkVisible = true
    },
    saveDispose(){
      let obj = {
        scrmBlackVOList: this.selectedCustomerList,
        remark: this.remarkForm.remark
      }
      this.saveLoading = true
      $api.addBlacklist(obj).then(res => {
        if(res.code == 200){
          this.$message.success('添加成功')
          this.selectCustomerVisible = false
          this.remarkVisible = false
          this.handleQuery()
        }
        this.saveLoading = false
      }).catch(() => {
        this.saveLoading = false
      })
    },
    deleteConfirm() {
      this.delloading = true
      let arr = this.selectedList.map(item => item.blackId)
      $api.removeBlackList({blackIdList: arr}).then(res => {
        if(res.code == 200){
          this.$message.success('删除成功')
          this.delvisible = false
          this.handleQuery()
        }
        this.delloading = false
      }).catch(() => {
        this.delloading = false
      })
    },
  },
  mounted() {
    api.getList({
      name: '',
      department: '',
      pageNum: 1,
      pageSize: 99999,
      isActivate: '',
    })
      .then(({rows}) => {
        const data = rows.map(item => {
          return {
            dictLabel: item.name,
            dictValue: item.userId,
          }
        })
        this.enumOpt.weUserIdList = JSON.parse(JSON.stringify(data))
      })
    this.handleQuery()
  },
  created() {
  },
}

</script>

<style lang="scss" scoped>

.app-container {
  height: 100%;
}

// table 右上角的操作按钮
.right-btn {
  padding: 16px 0;
  margin-right: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
